//
//  HBMyMsgDetailCtrl.m
//  HuoBao
//
//  Created by chaiyanan on 15/12/7.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBMyMsgDetailCtrl.h"

@interface HBMyMsgDetailCtrl ()
<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *myWebView;

//下面这些不用了
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UILabel *titleLable;
@property (nonatomic, strong) UILabel *subTitleLable;
@property (nonatomic, strong) UILabel *timeLable;

@end

@implementation HBMyMsgDetailCtrl

#pragma mark - system

- (void)viewDidLoad
{
    [super viewDidLoad];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
}

- (void)buildLayout
{
    [super buildLayout];
    self.title = @"消息详情";
//    [self createCustomView];
    
    self.view.backgroundColor = [LCCommon getColor:Main_BackGround_Color];
    self.myWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    self.myWebView.delegate = self;
    [self.view addSubview:self.myWebView];
    
    self.myWebView.backgroundColor = [LCCommon getColor:Main_BackGround_Color];
    self.myWebView.opaque = NO;
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    [self.myWebView stopLoading];
    self.myWebView.delegate = nil;
    [self.myWebView removeFromSuperview];
    self.myWebView = nil;
}

#pragma mark - delegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    LLog(@"%@,%@",request.URL.absoluteString,self.webUrl);
    if ([request.URL.absoluteString isEqualToString:self.webUrl]||[request.URL.absoluteString isEqualToString:[NSString stringWithFormat:@"%@/",self.webUrl]])
    {
        [CustomLoadingView showHudToView:self.view];
    }
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [CustomLoadingView hideHudForView:self.view];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [CustomLoadingView hideHudForView:self.view];
}



#pragma mark - private


- (void)createCustomView
{
    //86*102
    
    self.scrollView = [self createScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) contentSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT-64) andSuperView:self.view];
    
    self.titleLable = [self createLableWithTextColor:[LCCommon getColor:@"d2d2d2"] font:[UIFont systemFontOfSize:18.0f] text:@"恭喜获得10000元体验金(点击领取)" textAlignment:NSTextAlignmentLeft frame:CGRectMake(15, 20, SCREEN_WIDTH-30, 18.0f) superView:self.scrollView numberOfLines:1];
    
    self.timeLable = [self createLableWithTextColor:[LCCommon getColor:@"666666"] font:[UIFont systemFontOfSize:13.0f] text:@"2015-12-30  11:45" textAlignment:NSTextAlignmentLeft frame:CGRectMake(15, self.titleLable.y+self.titleLable.height+11, SCREEN_WIDTH-30, 13.0f) superView:self.scrollView numberOfLines:1];
    
    self.subTitleLable = [self createLableWithTextColor:[LCCommon getColor:@"999999"] font:[UIFont systemFontOfSize:13.0f] text:@"好玩好赚-送体验金官方，赢了随时换钱，刺激火爆-送体验金下载，好赢就是硬道理！送体验金官方，24小时客服兑分服务，网络顶级送体验金平台" textAlignment:NSTextAlignmentLeft frame:CGRectMake(15, self.timeLable.y+self.timeLable.height+24, SCREEN_WIDTH-30, 50.0f) superView:self.scrollView numberOfLines:0];
    
    
    [self fitSubLableSize:self.subTitleLable];
    
    
}

- (UIScrollView *)createScrollViewWithFrame:(CGRect)frame contentSize:(CGSize)contentSize andSuperView:(UIView *)superView
{
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:frame];
    scrollView.contentSize = contentSize;
    scrollView.bounces = NO;
    [superView addSubview:scrollView];
    return scrollView;
}


- (UILabel *)createLableWithTextColor:(UIColor *)color font:(UIFont *)font text:(NSString *)text textAlignment:(NSTextAlignment)textAlignment frame:(CGRect)frame superView:(UIView *)superView numberOfLines:(NSInteger)numberOfLines
{
    UILabel *lable = [[UILabel alloc]initWithFrame:frame];
    lable.textColor = color;
    lable.font = font;
    lable.text = text;
    lable.textAlignment = textAlignment;
    lable.numberOfLines = numberOfLines;
    [superView addSubview:lable];
    return lable;
}

- (CGSize)fitSubLableSize:(UILabel *)lable
{
    CGRect lableFrame = lable.frame;
    
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:lable.text];;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:8];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, lable.text.length)];
    lable.attributedText = attributedString;
    [lable sizeToFit];
    
    CGRect lableFrameNew = lable.frame;
    lableFrameNew.origin = lableFrame.origin;
    lable.frame = lableFrameNew;
    
    if (lableFrameNew.origin.y+lableFrameNew.size.height+20>SCREEN_HEIGHT-64)
    {
        self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, lableFrameNew.origin.y+lableFrameNew.size.height+20);
    }
    
    return lableFrameNew.size;
}

- (void)setTitle:(NSString *)title andSubTitle:(NSString *)subTitle
{
    self.titleLable.text = title;
    self.subTitleLable.text = subTitle;
    [self fitSubLableSize:self.subTitleLable];
}


@end
